package com.youlu.campus.service.activity.impl;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityRecieveRule;
import com.youlu.campus.service.activity.ActivityRecieveRuleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class ActivityRecieveRuleServiceImpl implements ActivityRecieveRuleService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ActivityRecieveRule findByActivityId(String activityId) {
        log.info(":>>> 开始获取证书发放规则:{}", activityId);
        if (StringUtils.isBlank(activityId)) {
            log.error(":>>> 活动ID为空");
            throw new BusinessException("活动ID为空");
        }
        Query query = new Query().addCriteria(Criteria.where("activityId").is(activityId));
        ActivityRecieveRule rule = mongoTemplate.findOne(query, ActivityRecieveRule.class);
        log.info(":>>> 活动:{} 证书领取规则:{}", activityId, JSON.toJSONString(rule));
        return rule;
    }
}
